﻿/**************************************************************************
创建日期:	2019/10/30 14:51:36     
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com
创建时间:	
Copyright (c)  ZhCun.cn

描	述：
记	录：
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using ZhCun.DbCore.Cores;
using ZhCun.WebCommon.AL;
using ZhCun.WebCommon.ViewModels;
using MyConsume.Models;
using ZhCun.Utils.Helpers;
using MyConsume.AL.ViewModels;

namespace MyConsume.AL
{
    public class ALConsume : ALCRUD<ConsumeArgGetList, ConsumeArgSave, VConsume>
    {
        private bool Save(ConsumeArgSave arg, bool isAdd, out string msg)
        {
            if (isAdd)
            {
                PConsumeAdd p = new PConsumeAdd();
                p.ArgAccountId = arg.AccountId;
                p.ArgAmount = arg.Amount;
                p.ArgAmountType = arg.AmountType;
                p.ArgConsumeDate = arg.ConsumeDate;
                p.ArgConsumeRemark = arg.ConsumeRemark;
                p.ArgConsumeTypeId = arg.ConsumeTypeId;
                DB.ExecProcedure(p).ExecProc();
                msg = p.OutMsg;
                return p.OutCode == 0;
            }
            else
            {
                TConsume t = new TConsume();
                //t.AmountType = arg.AmountType;
                t.ConsumeDate = arg.ConsumeDate;
                t.ConsumeRemark = arg.ConsumeRemark;
                t.ConsumeTypeId = arg.ConsumeTypeId;
                var r =DB.Update(t, s => s.Id == arg.Id);
                var u = r.RowCount == 1;
                msg = u ? "更新成功" : "更新失败";
                return u;
            }
        }

        public override bool Add(ConsumeArgSave model, out string msg)
        {
            return Save(model, true, out msg);
        }

        public override bool Update(ConsumeArgSave model, out string msg)
        {
            return Save(model, false, out msg);
        }

        public override bool Del(string id, out string msg)
        {
            throw new NotImplementedException("帐目明细不允许删除记录");
        }

        protected override void SetQueryOption(ConsumeArgGetList arg, QueryCondition<VConsume> query)
        {
            query.WhereAnd(s => s.ConsumeDate >= arg.StartTime && s.ConsumeDate <= arg.EndTime);
            if (!arg.searchValue.IsEmpty())
            {
                query.WhereAnd(s => s.WEx_Like(s.ConsumeRemark, $"%{arg.searchValue}%"));
            }
        }
    }
}